<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/18
 * Time: 18:05
 */

namespace App\Http\Controllers\Api;


use App\Models\Order;
use App\Service\OrderService;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;

class OrderController extends BaseController
{
    public function getOrder()
    {
        $token = $this->request->header('token');
        $memberInfo = Cache::get($token);
        $params = $this->request->post();
        $limit = $this->request->post('limit',10);
        $page = $this->request->post('page',1);

        return OrderService::getApiList($params,$page,$limit,$memberInfo);

    }

    /**
     * 下单
     * @return \Illuminate\Http\JsonResponse
     */
    public function createOrder()
    {
        try{
            $params = $this->request->post();
            if(!isset($params['water_type']) || empty(intval($params['water_type']))){
                throw new \Exception('请选择桶装水的品牌');
            }
            if(!isset($params['building_no']) || empty(intval($params['building_no']))){
                throw new \Exception('请选择公寓栋数');
            }
            if(!isset($params['floor_no']) || empty(intval($params['floor_no']))){
                throw new \Exception('请选择公寓的楼层');
            }
            if(!isset($params['room_no']) || empty(trim($params['room_no']))){
                throw new \Exception('请选择公寓楼层的房间编号');
            }
            if(!isset($params['num']) || empty(intval($params['num']))){
                throw new \Exception('请输入桶装水的数量');
            }
            if(intval($params['num']) > 2){
                //throw new \Exception('桶装水的数量最多2桶');
            }

            $save = OrderService::createOrder($params);
            if(!$save){
                throw new \Exception('下单失败');
            }

        }catch(\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }

        return Response()->json(['code'=>200,'msg'=>'下单成功','data'=>$save]);

    }

    /**
     * 订单详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail()
    {
        $orderId = $this->request->post('order_id');
        $token = $this->request->header('token');
        $memberInfo = Cache::get($token);
        $orderInfo = Order::with(['userItem','memberItem'])->where('id',$orderId)->where('mid',$memberInfo['id'])->first();
        if(!$orderInfo){
            return Response()->json(['code'=>201,'msg'=>'该订单不存在']);
        }
        return Response()->json(['code'=>201,'msg'=>'获取成功','data'=>$orderInfo->toArray()]);
    }


    /**
     * 支付
     * @return \Illuminate\Http\JsonResponse
     */
    public function payOrder()
    {
        try{
            $orderId = $this->request->post('order_id');
            if(empty(intval($orderId))){
                return Response()->json(['code'=>201,'msg'=>'请求参数错误']);
            }
            $token = $this->request->header('token');
            $memberInfo = Cache::get($token);
            $result = (new WechatController())->payOrder($orderId,$memberInfo);
            if(!$result){
                throw new \Exception('支付出错');
            }
        }catch(\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'支付成功','data'=>$result]);
    }
    /**
     * 取消订单
     */
    public function cancelOrder()
    {
        $id = $this->request->post('id');
        if(!$id){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }
        return OrderService::cancelOrder($id);
    }


    public function sendDelivery()
    {
        $result = (new WechatController())->sendDelivery();
    }


}
